﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using log4net;
using IPR_StudyMeetingMrgSys.Enums;
using IPR_StudyMeetingMrgSys.Extension;
using IPR_StudyMeetingMrgSys.BLL;
using IPR_StudyMeetingMrgSys.Model;
using System.Reflection;
using Newtonsoft.Json;
using IPR_StudyMeetingMrgSys.Models;
using System.Data;


namespace IPR_StudyMeetingMrgSys.Controllers
{
    /// <summary>
    /// 考试答题操作控制器
    /// </summary>
    public class ExamAnswerController : ApiController
    {
        ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        /// <summary>
        /// 考试答题的增加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [Route("api/ExamAnswer/Add")]
        [HttpPost]
        public IHttpActionResult Add(Model.ExamAnswer model)
        {
            log.Info(JsonConvert.SerializeObject(model));

            ResultMsg resultMsg = new ResultMsg();
            try
            {

                //if (new BLL.ExamAnswer().Add(model) > 0)
                //{
                //    //返回token信息
                //    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                //    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                //    resultMsg.Data = "考试答题增加成功！";
                //}
                //else
                //{
                //    //返回token信息
                //    resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                //    resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                //    resultMsg.Data = "考试答题增加失败！";
                //}

            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = "考试答题增加异常！" + exc.Message;
            }
            return Json(resultMsg);
        }

        /// <summary>
        /// 考试答题查询
        /// </summary>
        /// <param name="swhere"></param>
        /// <param name="startindex"></param>
        /// <param name="endindex"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/ExamAnswer/Search")]
        public IHttpActionResult Search(string swhere, int startindex, int endindex)
        {
            ResultMsg resultMsg = new ResultMsg();
            try
            {
                bool blExist = false;
                DataSet dsSearch = new BLL.ExamAnswer().GetListByPage(swhere, "EAID", startindex, endindex);
                if (dsSearch != null)
                    if (dsSearch.Tables.Count > 0)
                        if (dsSearch.Tables[0].Rows.Count > 0)
                        {
                            //返回token信息
                            resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                            resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                            resultMsg.Data = dsSearch.Tables[0];
                            blExist = true;
                        }
                if (!blExist)
                {
                    //返回token信息
                    resultMsg.StatusCode = (int)StatusCodeEnum.NoData;
                    resultMsg.Info = StatusCodeEnum.NoData.GetEnumText();
                    resultMsg.Data = "考试答题不存在！";
                }
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = "查询异常！" + exc.Message;
            }
            return Json(resultMsg);
        }

        /// <summary>
        /// 考试答题编辑
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/ExamAnswer/Edit")]
        public IHttpActionResult Edit(Model.ExamAnswer model)
        {
            log.Info(JsonConvert.SerializeObject(model));

            ResultMsg resultMsg = new ResultMsg();
            try
            {
                //if (new IPR_StudyMeetingMrgSys.BLL.ExamAnswer().Update(model))
                //{
                //    //返回token信息
                //    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                //    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                //    resultMsg.Data = "考试答题编辑成功！";
                //}
                //else
                //{
                //    //返回token信息
                //    resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                //    resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                //    resultMsg.Data = "考试答题编辑失败！";
                //}
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = "考试答题编辑时出现异常！" + exc.Message;
            }
            return Json(resultMsg);
        }

        [HttpPost]
        [Route("api/ExamAnswer/Del")]
        public IHttpActionResult Del(Model.ExamAnswer model)
        {
            log.Info(JsonConvert.SerializeObject(model));

            ResultMsg resultMsg = new ResultMsg();
            try
            {
                if (new IPR_StudyMeetingMrgSys.BLL.ExamAnswer().Delete(model.EAID))
                {
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Data = "考试答题删除成功！";
                }
                else
                {
                    resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Data = "考试答题删除失败！";
                }
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Data = "考试答题删除时出现异常！" + exc.Message;
            }
            return Json(resultMsg);
        }

    }
}

